package com.rxt.thermopro.ui.fragment

import android.content.Intent
import android.graphics.Rect
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.app.common.base.BaseFragment
import com.rxt.thermopro.R
import com.rxt.thermopro.adapter.ContactListAdapter
import com.rxt.thermopro.bean.ContactBean
import com.rxt.thermopro.viewmodel.ContactViewModel
import kotlinx.android.synthetic.main.fragment_contact.*


class ContactFragment : BaseFragment<ContactViewModel>() {

    companion object {
        fun newInstance(): ContactFragment {
            return ContactFragment()
        }
    }

    override fun layoutId(): Int {
        return R.layout.fragment_contact
    }

    private lateinit var adapter: ContactListAdapter
    private val contactList = mutableListOf<ContactBean>()

    override fun initView(savedInstanceState: Bundle?) {
        adapter = ContactListAdapter(contactList)
        recyclerContact.layoutManager = LinearLayoutManager(requireContext())
        recyclerContact.adapter = adapter
        recyclerContact.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                if (parent.getChildLayoutPosition(view) == 1) {
                    outRect.top = resources.getDimensionPixelSize(R.dimen.item_margin)
                    outRect.bottom = resources.getDimensionPixelSize(R.dimen.item_margin)
                }else if (parent.getChildLayoutPosition(view) > 2){
                    outRect.top = 1
                }
            }
        })

        mViewModel.contactListData.observe(this, Observer {
            adapter.clear()
            adapter.addAll(it)
        })

        initListener()

        mViewModel.initData()
    }

    private fun initListener() {
        backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

        adapter.setOnItemClick(object : ContactListAdapter.OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                if (adapter.get(position).viewType == ContactListAdapter.VIEW_TYPE_HEADER_EMAIL) {
                    val data = Intent(Intent.ACTION_SENDTO)
                    data.data = Uri.parse("mailto:" + adapter.get(position).contactNumber)
                    startActivity(data)
                } else if (adapter.get(position).viewType == ContactListAdapter.VIEW_TYPE_VIEW) {
                    val dialIntent = Intent(
                        Intent.ACTION_DIAL,
                        Uri.parse("tel:" + adapter.get(position).contactNumber)
                    ) //跳转到拨号界面，同时传递电话号码
                    startActivity(dialIntent)
                }
            }
        })
    }
}